/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jsf;

import entidades.MaterialesHormigon;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import jpa.MaterialesHormigonJpaController;
import jsf.util.JsfUtil;
import jpa.exceptions.NonexistentEntityException;
import entidades.MaterialesHormigonPK;
import jsf.util.PagingInfo;

/**
 *
 * @author dvasquez
 */
public class MaterialesHormigonController {

    public MaterialesHormigonController() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        jpaController = (MaterialesHormigonJpaController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "materialesHormigonJpa");
        pagingInfo = new PagingInfo();
        converter = new MaterialesHormigonConverter();
    }
    private MaterialesHormigon materialesHormigon = null;
    private List<MaterialesHormigon> materialesHormigonItems = null;
    private MaterialesHormigonJpaController jpaController = null;
    private MaterialesHormigonConverter converter = null;
    private PagingInfo pagingInfo = null;

    public PagingInfo getPagingInfo() {
        if (pagingInfo.getItemCount() == -1) {
            pagingInfo.setItemCount(jpaController.getMaterialesHormigonCount());
        }
        return pagingInfo;
    }

    public SelectItem[] getMaterialesHormigonItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(jpaController.findMaterialesHormigonEntities(), false);
    }

    public SelectItem[] getMaterialesHormigonItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(jpaController.findMaterialesHormigonEntities(), true);
    }

    public MaterialesHormigon getMaterialesHormigon() {
        if (materialesHormigon == null) {
            materialesHormigon = (MaterialesHormigon) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentMaterialesHormigon", converter, null);
        }
        if (materialesHormigon == null) {
            materialesHormigon = new MaterialesHormigon();
        }
        return materialesHormigon;
    }

    public String listSetup() {
        reset(true);
        return "materialesHormigon_list";
    }

    public String createSetup() {
        reset(false);
        materialesHormigon = new MaterialesHormigon();
        materialesHormigon.setMaterialesHormigonPK(new MaterialesHormigonPK());
        return "materialesHormigon_create";
    }

    public String create() {
        try {
            jpaController.create(materialesHormigon);
            JsfUtil.addSuccessMessage("MaterialesHormigon was successfully created.");
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return listSetup();
    }

    public String detailSetup() {
        return scalarSetup("materialesHormigon_detail");
    }

    public String editSetup() {
        return scalarSetup("materialesHormigon_edit");
    }

    private String scalarSetup(String destination) {
        reset(false);
        materialesHormigon = (MaterialesHormigon) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentMaterialesHormigon", converter, null);
        if (materialesHormigon == null) {
            String requestMaterialesHormigonString = JsfUtil.getRequestParameter("jsfcrud.currentMaterialesHormigon");
            JsfUtil.addErrorMessage("The materialesHormigon with id " + requestMaterialesHormigonString + " no longer exists.");
            return relatedOrListOutcome();
        }
        return destination;
    }

    public String edit() {
        materialesHormigon.getMaterialesHormigonPK().setId(materialesHormigon.getMaterialesHormigonPK().getId());
        String materialesHormigonString = converter.getAsString(FacesContext.getCurrentInstance(), null, materialesHormigon);
        String currentMaterialesHormigonString = JsfUtil.getRequestParameter("jsfcrud.currentMaterialesHormigon");
        if (materialesHormigonString == null || materialesHormigonString.length() == 0 || !materialesHormigonString.equals(currentMaterialesHormigonString)) {
            String outcome = editSetup();
            if ("materialesHormigon_edit".equals(outcome)) {
                JsfUtil.addErrorMessage("Could not edit materialesHormigon. Try again.");
            }
            return outcome;
        }
        try {
            jpaController.edit(materialesHormigon);
            JsfUtil.addSuccessMessage("MaterialesHormigon was successfully updated.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return listSetup();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return detailSetup();
    }

    public String destroy() {
        String idAsString = JsfUtil.getRequestParameter("jsfcrud.currentMaterialesHormigon");
        MaterialesHormigonPK id = converter.getId(idAsString);
        try {
            jpaController.destroy(id);
            JsfUtil.addSuccessMessage("MaterialesHormigon was successfully deleted.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return relatedOrListOutcome();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return relatedOrListOutcome();
    }

    private String relatedOrListOutcome() {
        String relatedControllerOutcome = relatedControllerOutcome();
        if (relatedControllerOutcome != null) {
            return relatedControllerOutcome;
        }
        return listSetup();
    }

    public List<MaterialesHormigon> getMaterialesHormigonItems() {
        if (materialesHormigonItems == null) {
            getPagingInfo();
            materialesHormigonItems = jpaController.findMaterialesHormigonEntities(pagingInfo.getBatchSize(), pagingInfo.getFirstItem());
        }
        return materialesHormigonItems;
    }

    public String next() {
        reset(false);
        getPagingInfo().nextPage();
        return "materialesHormigon_list";
    }

    public String prev() {
        reset(false);
        getPagingInfo().previousPage();
        return "materialesHormigon_list";
    }

    private String relatedControllerOutcome() {
        String relatedControllerString = JsfUtil.getRequestParameter("jsfcrud.relatedController");
        String relatedControllerTypeString = JsfUtil.getRequestParameter("jsfcrud.relatedControllerType");
        if (relatedControllerString != null && relatedControllerTypeString != null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Object relatedController = context.getApplication().getELResolver().getValue(context.getELContext(), null, relatedControllerString);
            try {
                Class<?> relatedControllerType = Class.forName(relatedControllerTypeString);
                Method detailSetupMethod = relatedControllerType.getMethod("detailSetup");
                return (String) detailSetupMethod.invoke(relatedController);
            } catch (ClassNotFoundException e) {
                throw new FacesException(e);
            } catch (NoSuchMethodException e) {
                throw new FacesException(e);
            } catch (IllegalAccessException e) {
                throw new FacesException(e);
            } catch (InvocationTargetException e) {
                throw new FacesException(e);
            }
        }
        return null;
    }

    private void reset(boolean resetFirstItem) {
        materialesHormigon = null;
        materialesHormigonItems = null;
        pagingInfo.setItemCount(-1);
        if (resetFirstItem) {
            pagingInfo.setFirstItem(0);
        }
    }

    public void validateCreate(FacesContext facesContext, UIComponent component, Object value) {
        MaterialesHormigon newMaterialesHormigon = new MaterialesHormigon();
        newMaterialesHormigon.setMaterialesHormigonPK(new MaterialesHormigonPK());
        String newMaterialesHormigonString = converter.getAsString(FacesContext.getCurrentInstance(), null, newMaterialesHormigon);
        String materialesHormigonString = converter.getAsString(FacesContext.getCurrentInstance(), null, materialesHormigon);
        if (!newMaterialesHormigonString.equals(materialesHormigonString)) {
            createSetup();
        }
    }

    public Converter getConverter() {
        return converter;
    }

}
